/*
 * @Author: your name
 * @Date: 2021-10-20 09:13:48
 * @LastEditTime: 2021-10-20 10:24:58
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \Vuec:\Users\86131\Desktop\project\payforknowledge\src\utils\http.js
 */

function choiceApi(id) {
  let BASE_URL;
  switch (id) {
    case 1:
      BASE_URL = "http://1.116.189.101:3000";
      break;
    case 2:
      BASE_URL = "http://39.96.77.250";
      break;
    case 3:
      BASE_URL = "http://www.pudge.wang:4000";
      break;
  }
  return BASE_URL;
}
const http = {
  get(url, params, id) {
    // url  ->  /area/list
    // params  ->  {a: 3, b: 4}
    const BASE_URL = choiceApi(id);
    // /area/list?a=3&b=4

    if (params) {
      const arr = Object.keys(params); // [a, b]
      const arr2 = arr.map((item) => {
        return item + "=" + params[item];
      }); // ["a=3", "b=4"]
      const str = arr2.join("&"); // "a=3&b=4"
      url = url + "?" + str;
    }

    return fetch(BASE_URL + url)
      .then((response) => response.json())
      .then((res) => {
        return res;
      });
  },
  post(url, data, id) {
    const BASE_URL = choiceApi(id);
    return fetch(BASE_URL + url, {
      method: "POST",
      body: JSON.stringify(data),
      headers: {
        "content-type": "application/json",
      },
    })
      .then((response) => response.json())
      .then((res) => {
        return res;
      });
  },
};

export default http;
